package com.chenbei.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chenbei.system.domain.SysUser;
import com.chenbei.system.domain.SysUserRole;
import com.chenbei.system.domain.SysRole;
import com.chenbei.system.mapper.SysUserRoleMapper;
import com.chenbei.system.service.ISysUserRoleService;
import com.chenbei.system.vo.req.SysRoleReq;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class SysUserRoleService extends ServiceImpl<SysUserRoleMapper, SysUserRole> implements ISysUserRoleService {

    @Transactional
    @Override
    public void userBindRoles(long userId, List<Long> roleIdList) {
        removeUserRoles(userId);
        if (roleIdList != null && !roleIdList.isEmpty()) {
            List<SysUserRole> sysUserRoleList = new ArrayList<>();
            for (Long roleId : roleIdList) {
                SysUserRole sysUserRole = new SysUserRole();
                sysUserRole.setUserId(userId);
                sysUserRole.setRoleId(roleId);

                sysUserRoleList.add(sysUserRole);
            }
            saveBatch(sysUserRoleList);
        }
    }

    @Transactional
    @Override
    public void roleBindUsers(long roleId, List<Long> userIdList) {
        removeRoleUsers(roleId);
        List<SysUserRole> sysUserRoleList = new ArrayList<>();
        for (Long userId : userIdList) {
            if (userId != 1) {
                SysUserRole sysUserRole = new SysUserRole();
                sysUserRole.setUserId(userId);
                sysUserRole.setRoleId(roleId);

                sysUserRoleList.add(sysUserRole);
            }
        }
        saveBatch(sysUserRoleList);
    }

    @Override
    public void removeUserRoles(long userId) {
        LambdaQueryWrapper<SysUserRole> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysUserRole::getUserId, userId);
        List<SysUserRole> list = list(lambdaQueryWrapper);
        if (list != null && !list.isEmpty()) {
            List<Long> bindIdList = list.stream().map(SysUserRole::getId).collect(Collectors.toList());
            batchDelete(bindIdList);
        }
    }

    @Override
    public void removeRoleUsers(long roleId) {
        LambdaQueryWrapper<SysUserRole> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysUserRole::getRoleId, roleId);
        List<SysUserRole> list = list(lambdaQueryWrapper);
        if (list != null && !list.isEmpty()) {
            List<Long> bindIdList = list.stream().map(SysUserRole::getId).collect(Collectors.toList()).stream().filter(id -> id != 1).collect(Collectors.toList());
            batchDelete(bindIdList);
        }
    }

    @Override
    public Page<SysUser> boundUsers(SysRoleReq sysRoleReq) {
        if (sysRoleReq.getRoleId() != null) {
            Page<SysUserRole> pageConfig = new Page<>(sysRoleReq.getPageNum(), sysRoleReq.getPageSize());
            return baseMapper.boundUsers(pageConfig, sysRoleReq);
        }
        return null;
    }

    private void batchDelete(Collection<Long> idList) {
        LambdaQueryWrapper<SysUserRole> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(SysUserRole::getId, idList);
        remove(lambdaQueryWrapper);
    }

    @Override
    public List<SysRole> listByAdminUserId(Long adminUserId) {
        return baseMapper.listByAdminUserId(adminUserId);
    }
}
